/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ronin.managed.bean.lazydatamodel;

import com.ronin.model.Firma;
import com.ronin.model.Musteri;
import com.ronin.model.kriter.MusteriSorguKriteri;
import com.ronin.service.IMusteriService;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author esimsek
 */
public class MusteriDataModel extends LazyDataModel<Musteri>{
    private List<Musteri> datasource;
    private int pageSize;
    private int rowIndex;
    private int rowCount;
   
    private IMusteriService musteriService;
    private MusteriSorguKriteri sorguKriteri;

    
    public MusteriDataModel(IMusteriService crudService,MusteriSorguKriteri sorguKriteri) {
    	this.sorguKriteri = sorguKriteri;
        this.musteriService = crudService;
   
    }
 
    @Override
    public List<Musteri> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
    	datasource = musteriService.getListCriteriaForPaging(first,pageSize,sorguKriteri);
    	setRowCount(musteriService.getCountByCriteriaForPaging(sorguKriteri));
        return datasource;
    }
 
    @Override
    public boolean isRowAvailable() {
        if(datasource == null)
            return false;
        int index = rowIndex % pageSize ;
        return index >= 0 && index < datasource.size();
    }
 
    @Override
    public Object getRowKey(Musteri musteri) {
        return musteri.getId().toString();
    }
 
    @Override
    public Musteri getRowData() {
        if(datasource == null)
            return null;
        int index =  rowIndex % pageSize;
        if(index > datasource.size()){
            return null;
        }
        return datasource.get(index);
    }
 
    @Override
    public Musteri getRowData(String rowKey) {
        if(datasource == null)
            return null;
       for(Musteri musteri : datasource) {
           if(musteri.getId().toString().equals(rowKey))
           return musteri;
       }
       return null;
    }
 
    @Override
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
 
    @Override
    public int getPageSize() {
        return pageSize;
    }
 
    @Override
    public int getRowIndex() {
        return this.rowIndex;
    }
 
    @Override
    public void setRowIndex(int rowIndex) {
        this.rowIndex = rowIndex;
    }
 
    @Override
    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }
 
    @Override
    public int getRowCount() {
        return this.rowCount;
    }
 
    @Override
    public void setWrappedData(Object list) {
        this.datasource = (List<Musteri>) list;
    }
 
    @Override
    public Object getWrappedData() {
        return datasource;
    }

    public List<Musteri> getDatasource() {
        return datasource;
    }

    public void setDatasource(List<Musteri> datasource) {
        this.datasource = datasource;
    }

    public IMusteriService getMusteriService() {
        return musteriService;
    }

    public void setMusteriService(IMusteriService musteriService) {
        this.musteriService = musteriService;
    }

    public MusteriSorguKriteri getSorguKriteri() {
        return sorguKriteri;
    }

    public void setSorguKriteri(MusteriSorguKriteri sorguKriteri) {
        this.sorguKriteri = sorguKriteri;
    }
    

}
